'use strict';

angular.module('core')

.directive('accordionToggle', function() {
    return {
        require: ['^accordion'],
        link: function (scope, element, attrs, controllers) {
            var accordionCtrl = controllers[0];
            accordionCtrl.registerToggle(element);

            element.on('click', function () {
                if (!attrs.disabled) {
                    var index = attrs.bsCollapseToggle && attrs.bsCollapseToggle !== 'bs-collapse-toggle' ? attrs.bsCollapseToggle : accordionCtrl.getToggleIndex(element);
                    accordionCtrl.setActive(index * 1);
                    scope.$apply();
                }
            });
        }
    };
});